/*
 * Copyright 2010 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen.sax;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import atma.jconfiggen.Expr;
import atma.jconfiggen.Param;

import static atma.jconfiggen.sax.SAXHandler.Elem;

class ConfigH extends SeqH
{
	private static final AttrMatcher impAttr = new AttrMatcher("import","c");
	private static final AttrMatcher paramAttr = new AttrMatcher("param","type","id");

	private final ArrayList<String> imps;
	private final ArrayList<Param> params;

	ConfigH(SAXHandler s,ArrayList<String> i,ArrayList<Param> p,ArrayList<Expr> e) {super(s,e);imps = i;params = p;}

	ElemH elem(Elem e,Attributes attr) throws SAXException
	{
		if (e == Elem.IMPORT)
		{
			String[] atv = impAttr.match(saxh.loc,attr);
			if (atv[0] == null) missAttr("import","c");
			imps.add(atv[0]);
			return new ElemH(saxh);
		}
		if (e == Elem.PARAM)
		{
			String[] atv = paramAttr.match(saxh.loc,attr);
			if (atv[0] == null) missAttr("param","type");
			if (atv[1] == null) missAttr("param","id");
			params.add(new Param(atv[0],atv[1]));
			return new ElemH(saxh);
		}
		return super.elem(e,attr);
	}
}
